<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Crm\Controller;
use Zend\View\Model\ViewModel;

class IndexController extends AbstractController
{
    public function indexAction()
    {
        return new ViewModel();
    }
    public function addCityAction()
    {
        $data = $this->request->getQuery();
        $city=array(
            'city_id'=>'',
            'city_name'=>'',
        );
        if(isset($data['id'])){
            $city=$this->getCityTable()->getItem($data['id']);
        }
//         var_dump($data);
//         die();
        return new ViewModel(array(
            'city'=>$city,
        ));
    }
    public function editCityAction()
    {
        $data = $this->request->getQuery();
        $city=array(
            'city_id'=>'',
            'city_name'=>'',
        );
        if(isset($data['id'])){
            $city=$this->getCityTable()->getItem($data['id']);
        }
//         var_dump($data);
//         die();
        return new ViewModel(array(
            'city'=>$city,
        ));
    }
    
    public function deleteCityAction()
    {
        $request = $this->getRequest();
        $city_id = $request->getPost('city_id');
        try{
            $this->getCityTable()->deleteItem($city_id);
            $response=array(
                'result' => 'true',
                'reason' => '删除成功！',
            );
            die(json_encode($response));
        }catch (\Exception $e){
            $response=array(
                'result' => 'false',
                'reason' => '系统错误！',
            );
            die(json_encode($response));
        }
    }
    /*
     * 城市列表
     */
    public function ajaxCityListAction()
    {
        $post = $this->request->getPost()->toArray();
        $paginator = $this->getCityTable()->getCityList($post);
        
        //var_dump($paginator);
        $result = array(
            'paginator' => $paginator,
        );
        
        $viewModel = new ViewModel();
        $viewModel->setVariables($result)
        ->setTerminal(true);
        
        return $viewModel;
        
    }
    
    public function insertCityAction()
    {
        $cityTable = $this->getCityTable();
        $data = $this->request->getPost()->toArray();
        $city = array('city_name'=>$data['city_name']);
        try{
            $cityTable->addItem($city);
            $this->flashMessenger()->addSuccessMessage("新增城市成功！");
            return $this->redirect()->toRoute('crm/index');
        }catch(\Exception $e)
        {
            //die($e);
            $this->flashMessenger()->addErrorMessage("新增城市失败！");
            return $this->redirect()->toRoute('crm/index');
        }
        
    }
    public function updateCityAction()
    {
        $cityTable = $this->getCityTable();
        $data = $this->request->getPost()->toArray();
        $city = array('city_name'=>$data['city_name']);
        try{
            $cityTable->updateItem($data['city_id'],$city);
            $this->flashMessenger()->addSuccessMessage("修改城市成功！");
            return $this->redirect()->toRoute('crm/index');
        }catch(\Exception $e)
        {
            //die($e);
            $this->flashMessenger()->addErrorMessage("修改城市失败！");
            return $this->redirect()->toRoute('crm/index');
        }
        
    }
    public function catalogAction()
    {
        $cityList = array();
        $cityList = $this->getCityTable()->find();
        return new ViewModel(array(
            'city'=>$cityList,
        ));
    }
    public function addCatalogAction()
    {
        $data = $this->request->getQuery();
        $catalog=array(
            'catalog_id'=>'',
            'city_id'=>'',
            'catalog_name'=>'',
            'catalog_description'=>'',
        );
        if(isset($data['id'])){
            $catalog=$this->getCatalogTable()->getItem($data['id']);
        }
        //         var_dump($data);
        //         die();
        
        $cityList = array();
        $cityList = $this->getCityTable()->find();
        return new ViewModel(array(
            'catalog'=>$catalog,
            'city'=>$cityList,
        ));
    }
    public function editCatalogAction()
    {
        $data = $this->request->getQuery();
        $catalog=array(
            'catalog_id'=>'',
            'city_id'=>'',
            'catalog_name'=>'',
            'catalog_description'=>'',
        );
        if(isset($data['id'])){
            $catalog=$this->getCatalogTable()->getItem($data['id']);
        }
        //         var_dump($data);
        //         die();
        
        $cityList = array();
        $cityList = $this->getCityTable()->find();
        return new ViewModel(array(
            'catalog'=>$catalog,
            'city'=>$cityList,
        ));
    }
    
    public function deleteCatalogAction()
    {
        $request = $this->getRequest();
        $catalog_id = $request->getPost('catalog_id');
        try{
            $this->getCatalogTable()->deleteItem($catalog_id);
            $response=array(
                'result' => 'true',
                'reason' => '删除成功！',
            );
            die(json_encode($response));
        }catch (\Exception $e){
            $response=array(
                'result' => 'false',
                'reason' => '系统错误！',
            );
            die(json_encode($response));
        }
    }
    /*
     * 商户类型列表
     */
    public function ajaxCatalogListAction()
    {
        $post = $this->request->getPost()->toArray();
        $paginator = $this->getCatalogTable()->getCatalogList($post);
    
        //var_dump($paginator);
        $result = array(
            'paginator' => $paginator,
        );
    
        $viewModel = new ViewModel();
        $viewModel->setVariables($result)
        ->setTerminal(true);
    
        return $viewModel;
    
    }
    
    public function updateCatalogAction()
    {
        $catalogTable = $this->getCatalogTable();
        $data = $this->request->getPost()->toArray();
        $catalog = array(
            'city_id'=>$data['city_id'],
            'catalog_name'=>$data['catalog_name'],
            'catalog_description'=>$data['catalog_description'],
        );
        try{
            $catalogTable->updateItem($data['catalog_id'],$catalog);
            $this->flashMessenger()->addSuccessMessage("商户类型修改成功！");
            return $this->redirect()->toRoute('crm/index',array('action'=>'catalog'));
        }catch(\Exception $e)
        {
            //die($e);
            $this->flashMessenger()->addErrorMessage("商户类型修改失败！");
            return $this->redirect()->toRoute('crm/index',array('action'=>'catalog'));
        }
    
    }
    public function insertCatalogAction()
    {
        $catalogTable = $this->getCatalogTable();
        $data = $this->request->getPost()->toArray();
        $catalog = array(
            'city_id'=>$data['city_id'],
            'catalog_name'=>$data['catalog_name'],
            'catalog_description'=>$data['catalog_description'],
        );
        try{
            $catalogTable->addItem($catalog);
            $this->flashMessenger()->addSuccessMessage("商户类型新增成功！");
            return $this->redirect()->toRoute('crm/index',array('action'=>'catalog'));
        }catch(\Exception $e)
        {
            //die($e);
            $this->flashMessenger()->addErrorMessage("商户类型新增失败！");
            return $this->redirect()->toRoute('crm/index',array('action'=>'catalog'));
        }
    
    }
}
